<template>
  <div class="login-container">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" class="box">
      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="old_password" label="旧密码">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="temp.old_password"
            :type="passwordType"
            placeholder="请输入旧密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @blur="capsTooltip = false"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-tooltip v-model="capsTooltip1" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password" label="新密码">
          <el-input
            :key="passwordType1"
            ref="password1"
            v-model="temp.password"
            :type="passwordType1"
            placeholder="请输入新的密码"
            name="password1"
            tabindex="2"
            autocomplete="on"
            @blur="capsTooltip1 = false"
          />
          <span class="show-pwd" @click="showPwd1">
            <svg-icon :icon-class="passwordType1 === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-tooltip v-model="capsTooltip2" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="re_password" label="确认新密码">
          <el-input
            :key="passwordType2"
            ref="password2"
            v-model="temp.re_password"
            :type="passwordType2"
            placeholder="请再次输入新的密码"
            name="password2"
            tabindex="2"
            autocomplete="on"
            @blur="capsTooltip2 = false"
          />
          <span class="show-pwd" @click="showPwd2">
            <svg-icon :icon-class="passwordType2 === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;letter-spacing: 80px">
      <el-button type="primary" :icon="listLoading1" @click="updateData">
        确定修改
      </el-button>
    </div>
  </div>
</template>

<script>
import { changePassword } from '@/api/user'
import { MessageBox, Message } from 'element-ui'
import waves from '@/directive/waves' // waves directive

export default {
  name: 'Reset',
  directives: { waves },
  data() {
    return {
      listLoading1: 'el-icon-edit',
      passwordType: 'password',
      capsTooltip: false,
      passwordType1: 'password',
      capsTooltip1: false,
      passwordType2: 'password',
      capsTooltip2: false,
      temp: {
        old_password: '',
        password: '',
        re_password: ''
      },
      rules: {
        old_password: [{ required: true, message: '旧密码是必需的', trigger: 'blur' }],
        password: [{ required: true, message: '新密码是必需的', trigger: 'blur' }],
        re_password: [{ required: true, message: '确认密码是必需的', trigger: 'blur' }]
      }
    }
  },
  methods: {
    resetTemp() {
      this.temp = {
        old_password: '',
        password: '',
        re_password: ''
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showPwd1() {
      if (this.passwordType1 === 'password') {
        this.passwordType1 = ''
      } else {
        this.passwordType1 = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password1.focus()
      })
    },
    showPwd2() {
      if (this.passwordType2 === 'password') {
        this.passwordType2 = ''
      } else {
        this.passwordType2 = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password2.focus()
      })
    },
    updateData() {
      MessageBox.confirm('确定要修改密码吗？', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.temp.password !== this.temp.re_password) {
              Message({
                message: '两次输入的新密码不一致，请检查',
                type: 'error',
                duration: 2000
              })
              return
            }
            this.listLoading1 = 'el-icon-loading'
            changePassword(this.temp).then(response => {
              this.listLoading1 = 'el-icon-edit'
              if (response.code === 1) {
                this.resetTemp()
                Message({
                  message: response.msg || '修改成功',
                  type: 'success',
                  duration: 2000
                })
              } else {
                Message({
                  message: response.msg || '修改失败',
                  type: 'error',
                  duration: 2000
                })
              }
            })
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0;
    font-size: 16px;
    color: #ccc;
    cursor: pointer;
    user-select: none;
  }
  .login-container {
    min-height: 100%;
    width: 100%;
  }
  .box{
    margin:100px 400px;
  }
  @media (max-width:550px) {
    .box {
      margin:50px 20px;
    }
  }
</style>
